<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="表单Key" prop="formKey">
        <el-input
          v-model="queryParams.formKey"
          clearable
          placeholder="请输入表单Key"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表单名称" prop="formName">
        <el-input
          v-model="queryParams.formName"
          clearable
          placeholder="请输入表单名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表单类型" prop="formType">
        <el-select
          v-model="queryParams.formType"
          clearable
          placeholder="表单类型"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in formTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:form:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:form:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column v-if="false" align="center" label="主键" prop="formId"/>
      <el-table-column align="center" label="表单Key" prop="formKey"/>
      <el-table-column align="center" label="表单名称" prop="formName"/>
      <el-table-column :formatter="typeFormat" align="center" label="表单类型" prop="formType"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="表单描述" prop="formDesc"/>
      <el-table-column align="center" label="表单内容" prop="formJson" show-overflow-tooltip/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:form:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['system:form:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { delForm, listForm } from '@/api/tool/form'
import FormGenerator from '@/views/tool/build'

export default {
  name: 'Form',
  components: {
    FormGenerator
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表单类型
      formTypeOptions: [],
      // 自定义表单 表格数据
      formList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        formKey: undefined,
        formName: undefined,
        formType: undefined
      }
    }
  },
  created() {
    this.getDicts('custom_form_type').then(response => {
      this.formTypeOptions = response.data
    })
    this.getList()
  },
  watch: {
    '$route'() {
      this.getList()
    }
  },
  methods: {
    /** 查询自定义表单 列表 */
    getList() {
      this.loading = true
      listForm(this.queryParams).then(response => {
        this.formList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.formTypeOptions, row.formType)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.formId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: '/form/build/0'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: '/form/build/' + row.formId
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const formIds = row.formId || this.ids
      this.$confirm('是否确认删除自定义表单 编号为"' + formIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delForm(formIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
